package test

import (
	"context"
	"fmt"
	"go-likaladi-shop/common/base_util"
	"lw-shop/goods_service/proto"
)

// TestGoodsList 测试获取用户列表，并验证密码
func TestGoodsList(goodsClient proto.GoodsClient) {
	// 调用 gRPC 服务的 GetUserList 方法，获取用户列表
	rsp, err := goodsClient.GoodsList(context.Background(), &proto.GoodsFilterRequest{
		KeyWords:    "翅烤",
		Pages:       1, // 分页参数：第 1 页
		PagePerNums: 2, // 每页 2 条数据
	})
	if err != nil {
		// 如果调用失败，则终止程序，并打印错误信息
		panic(err)
	}

	fmt.Println("总数量：", rsp.Total)

	// 遍历返回的用户数据
	for _, goods := range rsp.Data {
		// 打印用户信息
		fmt.Println(goods)
	}
}

func TestBatchGetGoods(goodsClient proto.GoodsClient) {
	ids := make([]int32, 3)
	ids[0] = 421
	ids[1] = 422
	ids[2] = 423

	rsp, err := goodsClient.BatchGetGoods(context.Background(), &proto.BatchGoodsIdInfo{Id: ids})
	if err != nil {
		panic(err)
	}

	fmt.Println("总数量：", rsp.Total)

	// 遍历返回的用户数据
	for _, goods := range rsp.Data {
		fmt.Println(goods)
	}
}

func TestCreateBrands(goodsClient proto.GoodsClient) {
	rsp, err := goodsClient.CreateBrand(context.Background(), &proto.BrandRequest{Name: "小米"})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp)
}

func TestUpdateBrands(goodsClient proto.GoodsClient) {
	rsp, err := goodsClient.UpdateBrand(context.Background(), &proto.BrandRequest{Id: 1114, Name: "小米苏2"})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp)
}

func TestUpdateBrandsForSql(goodsClient proto.GoodsClient) {
	rsp, err := goodsClient.UpdateBrandForSql(context.Background(),
		&proto.BrandRequest{Id: 1114, Name: "小米苏", Logo: "aa"})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp)
}

func TestDeleteBrands(goodsClient proto.GoodsClient) {
	rsp, err := goodsClient.DeleteBrand(context.Background(), &proto.BrandRequest{Id: 1115})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp)
}

func TestGetGoodsDetail(goodsClient proto.GoodsClient, goodsId int32) *proto.GoodsInfoResponse {
	rsp, err := goodsClient.GetGoodsDetail(context.Background(), &proto.GoodInfoRequest{
		Id: goodsId,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(base_util.ToJSON(rsp))
	return rsp
}

func TestCreateGoods(goodsClient proto.GoodsClient) {
	goods := TestGetGoodsDetail(goodsClient, 421)

	var reqGoods proto.CreateGoodsInfo

	base_util.Copy(goods, &reqGoods)

	reqGoods.BrandId = goods.Brand.Id
	reqGoods.Id = 0

	rsp, err := goodsClient.CreateGoods(context.Background(), &reqGoods)
	if err != nil {
		panic(err)
	}
	fmt.Println(base_util.ToJSON(rsp))
}

func TestUpdateGoods(goodsClient proto.GoodsClient) {
	goods := TestGetGoodsDetail(goodsClient, 421)
	var reqGoods proto.CreateGoodsInfo
	base_util.Copy(goods, &reqGoods)

	reqGoods.BrandId = goods.Brand.Id
	reqGoods.Id = 848

	rsp, err := goodsClient.UpdateGoods(context.Background(), &reqGoods)
	if err != nil {
		panic(err)
	}
	fmt.Println(base_util.ToJSON(rsp))
}
